package com.higgins.lawapp.client.maintenance.presenter;

import java.util.List;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.lawapp.client.maintenance.components.DataItemInstanceListBox;
import com.higgins.lawapp.client.maintenance.components.DataItemLevelListBox;
import com.higgins.lawapp.client.maintenance.components.DataItemTable;
import com.higgins.lawapp.client.maintenance.components.DataItemSourceListBox;
import com.higgins.lawapp.client.maintenance.handlers.DataItemTableHandler;
import com.higgins.lawapp.client.maintenance.handlers.DeleteButtonHandler;
import com.higgins.lawapp.client.maintenance.handlers.RetrieveButtonHandler;
import com.higgins.lawapp.client.maintenance.handlers.SendButtonHandler;
import com.higgins.lawapp.client.maintenance.model.DataItemModel;
import com.higgins.lawapp.client.settings.dataItemModel.DataItemDTO;
import com.higgins.lawapp.client.settings.dataItemModel.DataItemInstanceDTO;
import com.higgins.lawapp.client.settings.dataItemModel.DataItemLevelDTO;
import com.higgins.lawapp.client.settings.dataItemModel.DataItemSourceDTO;

public class DataItemPresenter {
	final ScrollPanel scrollPanel = new ScrollPanel();
	final DataItemModel dataItemModel = new DataItemModel(this);
	final DataItemTable dataItemTable = new DataItemTable();
	final DataItemInstanceListBox dataItemInstanceListBox = new DataItemInstanceListBox(dataItemModel, dataItemTable);
	final DataItemLevelListBox dataItemLevelListBox = new DataItemLevelListBox(dataItemModel, dataItemInstanceListBox, dataItemTable);
	final DataItemSourceListBox dataItemSourceListBox = new DataItemSourceListBox(dataItemModel, dataItemLevelListBox);
	final Button sendButton = new Button("Send");
	final Button retrieveButton = new Button("Refresh");
	final TextBox nameField = new TextBox();
	final DataItemTableHandler dataItemTableHandler = new DataItemTableHandler(this, dataItemModel, dataItemTable);
	final SendButtonHandler sendButtonHandler = new SendButtonHandler(this);
	final RetrieveButtonHandler retrieveButtonHandler = new RetrieveButtonHandler(this);
	final DeleteButtonHandler deleteButtonHandler = new DeleteButtonHandler(this);
	final DockLayoutPanel panel;
	
	public DataItemPresenter(DockLayoutPanel panel)	{
		this.panel = panel;
		nameField.setText("");
		sendButton.addStyleName("sendButton");
		retrieveButton.addStyleName("sendButton");
		panel.addNorth(dataItemSourceListBox, 2);
		panel.addNorth(dataItemLevelListBox, 2);
		panel.addNorth(dataItemInstanceListBox, 2);
		scrollPanel.add(dataItemTable);
		panel.add(scrollPanel);
		nameField.setFocus(true);
		nameField.selectAll();
		dataItemModel.retrieveRows();		
	}
	public void setDataItemDTOs(List<DataItemSourceDTO> dataItemSourceDTOs)		{
//		dataItemModel.setDataItemDTOs(dataItemSourceDTOs);
		dataItemSourceListBox.setDataItemSourceDTOs(dataItemSourceDTOs);
	}	
	public DataItemModel getDataItemModel()	{
		return dataItemModel;
	}
	public List<DataItemDTO> getDataItemDTOs()		{
		return dataItemModel.getDataItemDTOs();
	}
	public TextBox getNameField()	{
		return nameField;
	}
	public DataItemTable getDataItemTable()	{
		return dataItemTable;
	}
	public String getNameFieldText()	{
		return nameField.getText();
	}
	public void updateDataItemTable()	{
		nameField.setText("");
		retrieveButtonHandler.callRetrieve();
	}
//	public void setDataItemInstanceDTOs(List<DataItemInstanceDTO> dataItemInstancesDTOs)	{
//		if (dataItemInstancesDTOs.size() > 0)	
//			dataItemInstanceListBox.populateTable(dataItemInstancesDTOs.get(0));
//	}
	public void setDataItemContainer(List<DataItemDTO> dataItemDTOs, List<String> dataItemTitles)	{
		dataItemTable.setDataItemDTOs(dataItemDTOs, dataItemTitles);		
	}
}
